﻿/* Author: Achim Schnell */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppFx.Tokenization
{
    /// <summary>
    /// Matches string-symbols for tokens.
    /// </summary>
    public class StringSymbolVerifier : Verifier
    {
        #region Properties

        /// <summary>
        /// A list of all registered keywords.
        /// </summary>
        /// <value>
        /// The registered keywords.
        /// </value>
        public List<string> Symbols { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StringSymbolVerifier"/> class.
        /// </summary>
        /// <param name="tokenizer">The parent tokenizer.</param>
        public StringSymbolVerifier(ITokenizer tokenizer) : this(tokenizer, null){ }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringSymbolVerifier"/> class.
        /// </summary>
        /// <param name="tokenizer">The parent tokenizer.</param>
        /// <param name="symbols">The symbols.</param>
        public StringSymbolVerifier(ITokenizer tokenizer, params string[] symbols)
            : base(tokenizer)
        {
            Symbols = new List<string>();
            foreach (string s in symbols)
            {
                Symbols.Add(s);
            }
        }

        #endregion

        #region Verifier

        /// <summary>
        /// Detects if the given char matches the token's pattern.
        /// </summary>
        /// <param name="c">Char to verify for tokenization.</param>
        /// <returns>
        /// A bool indicating if the given char matches the token's pattern.
        /// </returns>
        public override bool Verify(char c)
        {
            if (Buffer.Length == 0 && Symbols.Any(word => word.StartsWith(new string(new char[] {c}))))
                return true;
            else if (Symbols.Any(word => word.StartsWith(Buffer + new string(new char[] {c}))))
                return true;
            else 
                return false;
        }

        /// <summary>
        /// Creates the token based on the current input-pattern.
        /// </summary>
        /// <returns>
        /// An IToken containing the current input-pattern.
        /// </returns>
        protected override IToken CreateToken()
        {
            return new Token(Buffer, eTokenType.Symbol);
        }

        #endregion
    }
}
